package requests

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/contracts/validation"
	"github.com/spf13/cast"
	"goravel/app/models/common"
)

type SiteRequest struct {
	Title       string          `form:"title" json:"title"`
	Url         string          `form:"url" json:"url"`
	Tel         string          `form:"tel" json:"tel"`
	Email       string          `form:"email" json:"email"`
	Address     string          `form:"address" json:"address"`
	Wechat      string          `form:"wechat" json:"wechat"`
	QQ          string          `form:"qq" json:"qq"`
	Logo        string          `form:"logo" json:"logo"`
	Description string          `form:"description" json:"description"`
	Qr          string          `form:"qr" json:"qr"`
	Status      int             `form:"status" json:"status"`
	Coordinates common.CoordRes `form:"coordinates" json:"coordinates"`
}

func (r *SiteRequest) Authorize(ctx http.Context) error {
	return nil
}

func (r *SiteRequest) Rules(ctx http.Context) map[string]string {
	return map[string]string{
		"title":       "required|string",
		"tel":         "required|string",
		"email":       "required|string",
		"address":     "required|string",
		"wechat":      "required|string",
		"status":      "required",
		"coordinates": "required",
	}
}

func (r *SiteRequest) Messages(ctx http.Context) map[string]string {
	return map[string]string{
		"title.required":       "标题不能为空",
		"title.string":         "标题格式不正确",
		"tel.required":         "电话不能为空",
		"tel.string":           "电话格式不正确",
		"email.required":       "邮箱不能为空",
		"email.string":         "邮箱格式不正确",
		"address.required":     "地址不能为空",
		"address.string":       "地址格式不正确",
		"wechat.required":      "微信不能为空",
		"wechat.string":        "微信格式不正确",
		"status.required":      "状态不能为空",
		"coordinates.required": "坐标不能为空",
	}
}

func (r *SiteRequest) Attributes(ctx http.Context) map[string]string {
	return map[string]string{}
}

func (r *SiteRequest) PrepareForValidation(ctx http.Context, data validation.Data) error {
	val, exist := data.Get("status")
	if exist {
		r.Status = cast.ToInt(val)
	}
	return nil
}
